<?php

function get_nested_menu($pages, $parent_id, $step, $openones){	
	$r='';	
	
	$r.='<ul>';
	foreach($pages as $page){
			if($page->parent_id==$parent_id){
				
				$isopen=in_array($page->id,$openones);
				$isselected=$openones[0]==$page->id;
				$numchildren=$page->count_children();
				$haschildren=($numchildren>0);
				
				if($isselected){
					$linkclass='selected';
				}else{
					$linkclass='';
				}
				
				if($haschildren){
					if($isopen){
						$liclass='open';
					}else{
						$liclass='closed';
					}
				}else{
					$liclass='empty';
				}
				
				if($page->type=='navitree'){
					$liclass='navitree';
				}
				
								
				$r.='<li class="'.$liclass.'">';
				
				$meta=$page->get_meta_object();
				
				$r.= html::anchor(CONTDIR.'page/single/'.$page->id, $page->title, array('class'=>$linkclass));
				if($isopen){
					$r.=get_nested_menu($pages, $page->id, $step+1, $openones);
				}
				$r.='</li>';
			}			
	}
	
	$r.='</ul>';
	return $r;
}

echo get_nested_menu($pages->as_array(),  0, 0, $openones);
?>